package com.example.myapplication;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class SetActivity extends AppCompatActivity {
    Button backtomainbtn;
    Button clearthingbtn;
    Button initunitbtn;
    Button inittype1btn;
    Button initBackAllSetting;
    UserDAO dao;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        dao=new UserDAO(SetActivity.this);
        backtomainbtn=findViewById(R.id.btn_backtomainactivity);
        clearthingbtn=findViewById(R.id.btn_clearthingtable);
        initunitbtn=findViewById(R.id.btn_initunit);
        inittype1btn=findViewById(R.id.btn_inittype1);
        initBackAllSetting=findViewById(R.id.btn_initall);
        backtomainbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        clearthingbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder=new AlertDialog.Builder(SetActivity.this);
                builder.setTitle("注意！");
                builder.setMessage("确定要删除全部物品信息？该操作不可逆！");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dao.clearThing();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
                AlertDialog dialog=builder.create();
                dialog.show();
            }
        });
        initunitbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder=new AlertDialog.Builder(SetActivity.this);
                builder.setTitle("注意！");
                builder.setMessage("确定要重置单位列表吗？该操作不可逆！");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dao.initUnit();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
                AlertDialog dialog=builder.create();
                dialog.show();
            }
        });
        inittype1btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder=new AlertDialog.Builder(SetActivity.this);
                builder.setTitle("注意！");
                builder.setMessage("确定要重置类型列表吗？该操作不可逆！");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dao.initTypeFirst();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
                AlertDialog dialog=builder.create();
                dialog.show();
            }
        });

        initBackAllSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder=new AlertDialog.Builder(SetActivity.this);
                builder.setTitle("注意！");
                builder.setMessage("确定要恢复出厂设置吗？该操作不会删除物品的基础信息，但将导致物品类型、单位全部初始化。该操作不可逆！");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dao.updateIsFurstUse(true);
                        Toast.makeText(SetActivity.this,"重启应用后设置生效",Toast.LENGTH_LONG).show();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
                AlertDialog dialog=builder.create();
                dialog.show();
            }
        });

    }
}
